﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using MToolStudio.Render;
using Microsoft.Mtool.FSM;

namespace MToolStudio.UI
{
    public class UITool
    {
        protected FSMDesigner designer;
        protected static System.Drawing.Drawing2D.Matrix cachedTransform;
        public FSMDesigner Designer
        {
            get { return designer; }
        }
        public UITool(FSMDesigner designer)
        {
            this.designer = designer;
        }

        public virtual void OnMouseDown(object sender, MouseEventArgs e)
        {

        }

        public virtual void OnMouseUp(object sender, MouseEventArgs e)
        { }

        public virtual void OnMouseMove(object sender, MouseEventArgs e)
        { }

        public virtual void OnKeyPress(object sender, KeyPressEventArgs e)
        { }

        public virtual void OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                Designer.CurrentTool = new DefaultActionTool(Designer);
                Designer.FocusedObject = null;
                Designer.Refresh();
            }
        }

        public virtual void OnMouseScroll(object sender, MouseEventArgs e)
        { }

        public static void SetTransform(Graphics g)
        {
            if (cachedTransform != null)
                cachedTransform.Dispose();
            cachedTransform = g.Transform;
            cachedTransform.Invert();
        }

        public virtual void Render(Graphics g)
        { }

        protected static MouseEventArgs TransformToLocal(MouseEventArgs e)
        {
            //Logger.Instance.Verbose("PerTransform: {0}, {1}", e.X, e.Y);
            Point[] pt = new Point[] { new Point(e.X, e.Y) };
            if (cachedTransform != null)
                cachedTransform.TransformPoints(pt);
            //Logger.Instance.Verbose("PostTransform: {0}, {1}", pt[0].X, pt[0].Y);

            return new MouseEventArgs(e.Button, e.Clicks, pt[0].X, pt[0].Y, e.Delta);
        }

    }
}